package com.mastercard.assets.web.struts.actions;

import com.mastercard.assets.MasterCardException;
import com.mastercard.assets.jpa.TblCuenta;
import com.mastercard.assets.jpa.TblProducto;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.CatalogoNuevoItemForm;
import com.mastercard.assets.web.util.to.UserLogin;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class CrearBpsCrear 
        extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException, 
			MasterCardException
	{
            HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
                
                CatalogoNuevoItemForm f = ( CatalogoNuevoItemForm ) form ;
                
                TblProducto Producto = new TblProducto() ;
                TblCuenta Cuenta = new TblCuenta() ;
                                
                List< TblProducto > producto = MasterCardDAO.getActiveProductById( f.getCampoInt2() );
                List< TblCuenta > cuenta = MasterCardDAO.getActiveCountById( f.getCampoInt1() ) ;
                //String dbps = f.getCampo1( ) ;
                //String pd =  f.getCampo2( ) ;
                
                for ( TblProducto l : producto ){
                    Producto = l ;
                    
                }
                
                for ( TblCuenta c : cuenta ){
                    Cuenta = c ;
                }
                
                java.math.BigDecimal Dombps = new java.math.BigDecimal(f.getCampo1( )) ;
                java.math.BigDecimal Pd = new java.math.BigDecimal(f.getCampo2( )) ;
                java.math.BigDecimal Intl = new java.math.BigDecimal(f.getCampo3( )) ;
                java.math.BigDecimal Card = new java.math.BigDecimal(f.getCampo4( )) ;
                
                
			MasterCardDAO.addNewBps( Cuenta , Producto , Dombps , Pd , Intl ,Card ) ;
                        
                        user.setNextWorkArea( "/WEB-INF/pages/WorkArea/CrearBpsExito.jsp" ) ;
			
                        MasterCardDAO.closeEntityManager( ) ;
			return mapping.findForward( "success" ) ;		
                
        }
    
}
